package com.xxc.hs.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xxc.hs.AboutActivity;
import com.xxc.hs.LoginActivity;
import com.xxc.hs.R;
import com.xxc.hs.UpdatePwdActivity;
import com.xxc.hs.bean.UserInfo;


@SuppressWarnings("ALL")
public class UserFragment extends Fragment {

    private View rootView;
    private TextView tv_name, tv_exit;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_user, container, false);
        try {
            tv_name = rootView.findViewById(R.id.tv_name);
            tv_exit = rootView.findViewById(R.id.tv_exit);

            tv_exit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(getActivity())
                            .setTitle("温馨提示")
                            .setMessage("确定要退出吗")
                            .setPositiveButton("是", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    startActivity(new Intent(getActivity(), LoginActivity.class));
                                    getActivity().finish();
                                }
                            })
                            .setNegativeButton("否", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                }
                            })
                            .show();
                }
            });

            //修改密码事件
            rootView.findViewById(R.id.updatePwd).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), UpdatePwdActivity.class);
                    startActivity(intent);
                }
            });

            //关于APP
            rootView.findViewById(R.id.app_about).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), AboutActivity.class);
                    startActivity(intent);
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }

        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //设置用户数据
        try {
            UserInfo userInfo = UserInfo.getUserInfo();
            if (null != userInfo) {
                tv_name.setText(userInfo.getUsername());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}